"""
仪表盘路由
"""
from flask import Blueprint, request
from flask_jwt_extended import jwt_required
from utils import ApiResponse
from services.dashboard_service import DashboardService

api_bp = Blueprint('dashboard', __name__, url_prefix='/admin/dashboard')
dashboard_service = DashboardService()


@api_bp.route('/stats', methods=['GET'])
@jwt_required()
def get_stats():
    """获取仪表盘统计数据"""
    try:
        stats = dashboard_service.get_stats()
        return ApiResponse.success(data=stats)
    except Exception as e:
        return ApiResponse.error(msg=str(e), code=500)


@api_bp.route('/checkin-trends', methods=['GET'])
@jwt_required()
def get_checkin_trends():
    """获取入住趋势数据"""
    try:
        start_date = request.args.get('startDate')
        end_date = request.args.get('endDate')
        period = request.args.get('period', 'day')
        
        trends = dashboard_service.get_checkin_trends(
            start_date=start_date,
            end_date=end_date,
            period=period
        )
        
        return ApiResponse.success(data=trends)
    except Exception as e:
        return ApiResponse.error(msg=str(e), code=500)


@api_bp.route('/room-distribution', methods=['GET'])
@jwt_required()
def get_room_distribution():
    """获取房间类型分布数据"""
    try:
        distribution = dashboard_service.get_room_distribution()
        return ApiResponse.success(data=distribution)
    except Exception as e:
        return ApiResponse.error(msg=str(e), code=500)


@api_bp.route('/revenue-stats', methods=['GET'])
@jwt_required()
def get_revenue_stats():
    """获取收入统计数据"""
    try:
        start_date = request.args.get('startDate')
        end_date = request.args.get('endDate')
        
        stats = dashboard_service.get_revenue_stats(
            start_date=start_date,
            end_date=end_date
        )
        
        return ApiResponse.success(data=stats)
    except Exception as e:
        return ApiResponse.error(msg=str(e), code=500)


@api_bp.route('/latest-reservations', methods=['GET'])
@jwt_required()
def get_latest_reservations():
    """获取最新预订"""
    try:
        limit = request.args.get('limit', 10, type=int)
        reservations = dashboard_service.get_latest_reservations(limit=limit)
        return ApiResponse.success(data=reservations)
    except Exception as e:
        return ApiResponse.error(msg=str(e), code=500)


@api_bp.route('/upcoming-checkins', methods=['GET'])
@jwt_required()
def get_upcoming_checkins():
    """获取即将入住的预订"""
    try:
        days = request.args.get('days', 7, type=int)
        checkins = dashboard_service.get_upcoming_checkins(days=days)
        return ApiResponse.success(data=checkins)
    except Exception as e:
        return ApiResponse.error(msg=str(e), code=500)

